package com.aliyun.messagemgt.dto.vo.weixin;

import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Xiaohang Lv
 * @Date: 2019/5/15 16:43
 */
public class WeixinMpNewsSendModelVO extends WeixinSendModelVO {

    private MpNews mpNews;

    public WeixinMpNewsSendModelVO() {
        super.setMsgtype("mpnews");
        mpNews = new MpNews();
    }

    public MpNews getMpNews() {
        return mpNews;
    }

    public WeixinMpNewsSendModelVO setMpNews(MpNews mpNews) {
        this.mpNews = mpNews;
        return this;
    }

    public WeixinMpNewsSendModelVO setTitle(String title) {
        if (CollectionUtils.isEmpty(mpNews.getArticles())) {
            MpNews.Article article = new MpNews.Article();
            article.setTitle(title);
            mpNews.getArticles().add(article);
            return this;
        }
        mpNews.getArticles().get(0).setTitle(title);
        return this;
    }

    public WeixinMpNewsSendModelVO setThumb_media_id(String description) {
        if (CollectionUtils.isEmpty(mpNews.getArticles())) {
            MpNews.Article article = new MpNews.Article();
            article.setThumb_media_id(description);
            mpNews.getArticles().add(article);
            return this;
        }
        mpNews.getArticles().get(0).setThumb_media_id(description);
        return this;
    }

    public WeixinMpNewsSendModelVO setDigest(String url) {
        if (CollectionUtils.isEmpty(mpNews.getArticles())) {
            MpNews.Article article = new MpNews.Article();
            article.setDigest(url);
            mpNews.getArticles().add(article);
            return this;
        }
        mpNews.getArticles().get(0).setDigest(url);
        return this;
    }

    public WeixinMpNewsSendModelVO setContent_source_url(String picUrl) {
        if (CollectionUtils.isEmpty(mpNews.getArticles())) {
            MpNews.Article article = new MpNews.Article();
            article.setContent_source_url(picUrl);
            mpNews.getArticles().add(article);
            return this;
        }
        mpNews.getArticles().get(0).setContent_source_url(picUrl);
        return this;
    }

    public WeixinMpNewsSendModelVO setContent(String picUrl) {
        if (CollectionUtils.isEmpty(mpNews.getArticles())) {
            MpNews.Article article = new MpNews.Article();
            article.setContent(picUrl);
            mpNews.getArticles().add(article);
            return this;
        }
        mpNews.getArticles().get(0).setContent(picUrl);
        return this;
    }

    public WeixinMpNewsSendModelVO setAuthor(String picUrl) {
        if (CollectionUtils.isEmpty(mpNews.getArticles())) {
            MpNews.Article article = new MpNews.Article();
            article.setAuthor(picUrl);
            mpNews.getArticles().add(article);
            return this;
        }
        mpNews.getArticles().get(0).setAuthor(picUrl);
        return this;
    }


    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    static class MpNews {
        private List<Article> articles;

        MpNews() {
            articles = new ArrayList<>();
        }

        public List<Article> getArticles() {
            return articles;
        }

        public MpNews setArticles(List<Article> articles) {
            this.articles = articles;
            return this;
        }
        @Data
        static class Article {
            /**
             * title	是	标题，不超过128个字节，超过会自动截断（支持id转译）
             */
            private String title;
            /**
             * thumb_media_id	是	图文消息缩略图的media_id, 可以通过素材管理接口获得。此处thumb_media_id即上传接口返回的media_id
             */
            private String thumb_media_id;
            /**
             * author	否	图文消息的作者，不超过64个字节
             */
            private String author;
            /**
             * content_source_url	否	图文消息点击“阅读原文”之后的页面链接
             */
            private String content_source_url;
            /**
             * content	是	图文消息的内容，支持html标签，不超过666 K个字节（支持id转译）
             */
            private String content;
            /**
             * digest	否	图文消息的描述，不超过512个字节，超过会自动截断（支持id转译）
             */
            private String digest;

            @Override
            public String toString() {
                return ToStringBuilder.reflectionToString(this);
            }
        }

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this);
        }
    }

}
